package com.shop.cloud.service.hongfu;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shop.cloud.common.mybatis.BaseService;
import com.shop.cloud.common.mybatis.MybatisDao;
import com.shop.cloud.dao.hongfu.UserDao;
import com.shop.cloud.dao.hongfu.UserMemberDao;
import com.shop.cloud.domain.hongfu.BalanceHistory;
import com.shop.cloud.domain.hongfu.TransactionLog;
import com.shop.cloud.domain.hongfu.User;
import com.shop.cloud.domain.hongfu.UserMember;
import com.shop.cloud.domain.hongfu.UserVip;

/**
 * 
 */
@Service("userMemberService")
public class UserMemberService extends BaseService<UserMember> implements IUserMemberService{
    
    @Autowired
	private UserMemberDao userMemberDao;
    
    @Autowired
   	private UserDao userDao;
    
    @Autowired
    private ITransactionLogService transactionLogService;
    
    @Autowired
    private IBalanceHistoryService balanceHistoryService;
    
    @Autowired
    private UserVipService userVipService;

	@Override
	public MybatisDao<UserMember> getEntityDao() {
		return userMemberDao;
	}
	
	public int addMember(UserMember userMember) {
		
		//修改用户表的会员等级
		User user = userDao.get(userMember.getUserId());
		
		if(user!=null&&user.getBalance().floatValue()>0){
			if(user.getBalance().floatValue()<userMember.getPrice()){
				//
				return 2;
			}
		}
		
		int userLevel=Integer.parseInt(userMember.getLevel().replaceAll("[a-zA-Z]",""));
		if (userLevel>user.getUserLevel()) {
			user.setUserLevel(userLevel);
		}
	    
	  //赠送用户积分
	  	UserVip vip=new UserVip();
	  	vip.setLevel(userMember.getLevel().toString());
	  	List<UserVip> list=userVipService.find(vip);
	  	if (list.size()>0) {
	  			user.setPoints(user.getPoints()+(list.get(0).getPrice().intValue()*list.get(0).getIntegral()/100));
	  		}
	  	
	  	//会员卡额度累加
	  	user.setVipBalance(user.getVipBalance()+list.get(0).getPrice().intValue());
		user.setVipBalanceSum(user.getVipBalanceSum()+list.get(0).getPrice().intValue());
	  	userDao.update(user);
		
		//添加交易流水
		TransactionLog transactionLog = new TransactionLog();
		transactionLog.setType(3);
		transactionLog.setTradeNo(userMember.getTradeNo());
		transactionLog.setTradeAmount(userMember.getPrice());
		transactionLog.setCreateTime(new Timestamp(new Date().getTime()));
		transactionLog.setContent("购买"+getNumber(Integer.parseInt(userMember.getLevel().replaceAll("[a-zA-Z]","" )))+"星会员卡");
		transactionLog.setRemark(user.getNickname()+"购买"+getNumber(Integer.parseInt(userMember.getLevel().replaceAll("[a-zA-Z]","" )))+"星会员卡,消费"+userMember.getPrice()+"元");
		//新增一条交易流水记录
		transactionLogService.insert(transactionLog);
		
		//添加余额明细
		BalanceHistory balanceHistory = new BalanceHistory();
		balanceHistory.setUserId(userMember.getUserId());
		balanceHistory.setType(25);
		balanceHistory.setOrderNo(userMember.getTradeNo());
		balanceHistory.setCreateTime(new Timestamp((new Date()).getTime()));
		balanceHistory.setRemark(user.getNickname()+"购买"+getNumber(Integer.parseInt(userMember.getLevel().replaceAll("[a-zA-Z]","" )))+"星会员卡,消费"+userMember.getPrice()+"元");
		balanceHistory.setUserLevel(Integer.parseInt(userMember.getLevel().replaceAll("[a-zA-Z]","" )));
		//创建流水
		balanceHistoryService.insert(balanceHistory);
	
		return userMemberDao.insert(userMember);
	}
	
	public static String getNumber(int num){
		if (num==1) {
			return "一";
		}
		if (num==2) {
			return "二";
		}
		if (num==3) {
			return "三";
		}
		if (num==4) {
			return "四";
		}
		if (num==5) {
			return "五";
		}
		
		return null;
	}
    
}